﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Xajhzwb.Components.Data;
using Xajhzwb.Components.Util;

namespace Xajhzwb
{
    public partial class FormPublish : Form
    {
        public FormPublish()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 画面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormPublish_Load(object sender, EventArgs e)
        {
            try
            {
                // 加载树形数据
                loadTreeView();
            }
            catch(Exception ex)
            {
                
            }
        }

        /// <summary>
        /// 加载树形数据
        /// </summary>
        private void loadTreeView()
        {
            List<ClassData> classList = DataHelper.GetClassNameList(Const.ClassType.Publish.ToString());
            if(classList != null && classList.Count > 0)
            {
                foreach (ClassData classData in classList)
                {
                    TreeNode classNode = new TreeNode();
                    classNode.Name = "C" + classData.ClassID.ToString();
                    classNode.Text = classData.ClassName;
                    classNode.Tag = classData;

                    List<PublishData> publishDataList = DataHelper.GetPublishNameList(classData.ClassID);
                    if(publishDataList != null && publishDataList.Count > 0)
                    {
                        foreach (PublishData publishData in publishDataList)
                        {
                            TreeNode ruleNode = new TreeNode();
                            ruleNode.Name = publishData.ID.ToString();
                            ruleNode.Text = publishData.Name.ToString();
                            ruleNode.Tag = publishData;

                            classNode.Nodes.Add(ruleNode);
                        }
                    }

                    treeView.Nodes.Add(classNode);
                }
            }

            //// default selected
            //COnfig config = new COnfig();
            //if(config.RecMg != "")
            //{
            //    treeViewWebPost.SelectedNode.Name = config.RecMg;
            //}
        }

        private void loadPostData(int ID)
        {
            PublishData publishData = DataHelper.GePublishData(ID);
            if(publishData != null)
            {
                lblName.Text = publishData.Name;
                txtUrlChk.Text = publishData.UrlChk;
                txtUrlPost.Text = publishData.UrlPost;
                chkIsReply.Checked = publishData.IsReply;
                txtUrlReply.Text = publishData.UrlReply;

                txtTitleMin.Text = publishData.TitleMin.ToString();
                txtTitleMax.Text = publishData.TitleMax.ToString();
                txtContentMin.Text = publishData.ContentMin.ToString();
                txtContentMax.Text = publishData.ContentMax.ToString();
                chkIsUbb.Checked = publishData.IsUbb;
                chkIsCrlf.Checked = publishData.IsCrlf;
                cboCodePage.SelectedValue = publishData.CodePage;
                chkIsForcePost.Checked = publishData.IsForcePost;
                rboPostSort1.Checked = (publishData.PostSort == 1);
                rboPostSort2.Checked = (publishData.PostSort == 2);
                chkIsSelfChk.Checked = publishData.IsSelfChk;
                rboChkMode1.Checked = (publishData.ChkMode == 0);
                rboChkMode2.Checked = (publishData.ChkMode == 1);
                rboChkMode3.Checked = (publishData.ChkMode == 2);

                txtPostTitle.Text = publishData.PostTitle;
                txtPostContent.Text = publishData.PostContent;
                txtPostParas.Text = publishData.PostParas;

                txtPostOtherParas.Text = publishData.PostOtherParas;
                txtPostUsers.Text = publishData.PostUsers;

                chkIsFtp.Checked = publishData.IsFtp;
                chkIsFtpPasv.Checked = publishData.IsFtpPasv;
                txtFtpServer.Text = publishData.FtpServer;
                txtFtpPort.Text = publishData.FtpPort.ToString();
                txtFtpUser.Text = publishData.FtpUser;
                txtFtpPass.Text = publishData.FtpPass;
                txtFtpDir.Text = publishData.FtpDir;
                chkIsDelDownFile.Checked = publishData.IsDelDownFile;

                cboTranslLang.SelectedValue = publishData.TransLang;
                txtTransParas.Text = publishData.TransParas;

                chkIsLogin.Checked = publishData.IsLogin;
                txtLoginUrl.Text = publishData.LoginUrl;
                txtPostCookies.Text = publishData.PostCookies;
                txtPostOkCode.Text = publishData.PostOkCode;
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            btnApply_Click(sender, e);
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 登陆获取Cookies
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetCookies_Click(object sender, EventArgs e)
        {
            FormBrowser formBrower = new FormBrowser(txtLoginUrl.Text);
            formBrower.getFlag = 0;
            formBrower.rCookie = new FormBrowser.ReturnCookie(GetCookie);
            formBrower.ShowDialog();
            formBrower.Dispose();
        }

        private void GetCookie(string strCookie)
        {
            this.txtPostCookies.Text = strCookie;
        }


        /// <summary>
        /// 树选择后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                // load postdata
                if(treeView.SelectedNode != null)
                {
                    string ClassID = treeView.SelectedNode.Name.ToString();
                    if(ClassID != "" && ClassID.Substring(0, 1) != "C")
                    {
                        loadPostData(int.Parse(ClassID));
                    }
                }
            }
            catch(Exception ex)
            {
                DialogResult result = Utils.ShowMessageBox(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 应用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                PublishData publishData = new PublishData();

                publishData.UrlChk = txtUrlChk.Text;
                publishData.UrlPost = txtUrlPost.Text;
                publishData.IsReply = chkIsReply.Checked;
                publishData.UrlReply = txtUrlReply.Text;

                publishData.TitleMin = int.Parse(txtTitleMin.Text);
                publishData.TitleMax = int.Parse(txtTitleMax.Text);
                publishData.ContentMin = int.Parse(txtContentMin.Text);
                publishData.ContentMax = int.Parse(txtContentMax.Text);
                publishData.IsUbb = chkIsUbb.Checked;
                publishData.IsCrlf = chkIsCrlf.Checked;
                publishData.CodePage = cboCodePage.SelectedValue.ToString();
                publishData.IsForcePost = chkIsForcePost.Checked;

                if (rboPostSort1.Checked) publishData.PostSort = 1;
                if (rboPostSort2.Checked) publishData.PostSort = 2;

                publishData.IsSelfChk = chkIsSelfChk.Checked;

                if (rboChkMode1.Checked) publishData.ChkMode = 0;
                if (rboChkMode2.Checked) publishData.ChkMode = 1;
                if (rboChkMode3.Checked) publishData.ChkMode = 2;

                publishData.PostTitle = txtPostTitle.Text;
                publishData.PostContent = txtPostContent.Text;
                publishData.PostParas = txtPostParas.Text;
                publishData.PostOtherParas = txtPostOtherParas.Text;
                publishData.PostUsers = txtPostUsers.Text;

                publishData.IsFtp = chkIsFtp.Checked;
                publishData.IsFtpPasv = chkIsFtpPasv.Checked;
                publishData.FtpServer = txtFtpServer.Text;
                publishData.FtpPort = int.Parse(txtFtpPort.Text);
                publishData.FtpUser = txtFtpUser.Text;
                publishData.FtpPass = txtFtpPass.Text;
                publishData.FtpDir = txtFtpDir.Text;
                publishData.IsDelDownFile = chkIsDelDownFile.Checked;

                publishData.TransLang = int.Parse(cboTranslLang.SelectedValue.ToString());
                publishData.TransParas = txtTransParas.Text ;

                publishData.IsLogin = chkIsLogin.Checked;
                publishData.LoginUrl = txtLoginUrl.Text;
                publishData.PostCookies = txtPostCookies.Text;
                publishData.PostOkCode = txtPostOkCode.Text;

                DataHelper.UpdPublishData(publishData);
            }
            catch (Exception ex)
            {
                
            }
        }

        /// <summary>
        /// 启用独立回复地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkIsReply_CheckedChanged(object sender, EventArgs e)
        {
            txtUrlReply.Enabled = chkIsReply.Checked;
        }

        /// <summary>
        /// 启用模拟发布
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkIsLoginPost_CheckedChanged(object sender, EventArgs e)
        {
            bool isLogingPost = chkIsLogin.Checked;

            txtLoginUrl.Enabled = isLogingPost;
            btnGetCookies.Enabled = isLogingPost;
            txtPostCookies.Enabled = isLogingPost;
        }

        /// <summary>
        /// 添加组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addClassToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClassData classData = new ClassData();
            classData.ClassName = Utils.GetNewClassName(treeView);
            classData.ClassType = Const.ClassType.Publish.ToString();
            classData.Sort = 0;
            DataHelper.InsClassData(classData);

            TreeNode newNode = new TreeNode();
            newNode.Name = "C" + DataHelper.GetMaxClassID(Const.ClassType.Publish.ToString()).ToString();
            newNode.Text = classData.ClassName;

            treeView.BeginUpdate();
            treeView.Nodes.Add(newNode);
            treeView.EndUpdate();
            treeView.SelectedNode = newNode;
        }

        /// <summary>
        /// 添加规则
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addRuleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PublishData publishData = new PublishData();
            publishData.Name = Utils.GetNewRuleName(treeView);
            if (treeView.SelectedNode.Name.Substring(0, 1) != "C")
            {
                publishData.ClassID = int.Parse(treeView.SelectedNode.Parent.Name.Replace("C", ""));
            }
            else
            {
                publishData.ClassID = int.Parse(treeView.SelectedNode.Name.Replace("C", ""));
            }
            DataHelper.InsPublishData(publishData);

            TreeNode newNode = new TreeNode();
            newNode.Name = DataHelper.GetMaxPublishID().ToString();
            newNode.Text = publishData.Name;
            newNode.Tag = DataHelper.GePublishData(int.Parse(newNode.Name));

            treeView.BeginUpdate();
            treeView.Nodes[treeView.SelectedNode.Index].Nodes.Add(newNode);
            treeView.EndUpdate();
            treeView.SelectedNode = newNode;
        }

        /// <summary>
        /// 展开全部
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void extToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView.ExpandAll();
        }

        /// <summary>
        /// 收缩全部
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void colToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView.CollapseAll();
        }

        /// <summary>
        /// 重命名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView.SelectedNode.BeginEdit();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void delToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView.SelectedNode.Name.Substring(0, 1) != "C")
            {
                int ID = int.Parse(treeView.SelectedNode.Name);
                DataHelper.DelPublishData(ID);
            }

            if (this.treeView.SelectedNode.Name.Substring(0, 1) == "C")
            {
                int classID = int.Parse(treeView.SelectedNode.Name.Substring(1));
                DataHelper.DelClassData(classID);
            }

            treeView.Nodes.Remove(this.treeView.SelectedNode);
        }

        /// <summary>
        /// 导入规则
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void impToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormImpExp formImpExp = new FormImpExp(true, Const.ClassType.Publish, 0);
            formImpExp.ShowDialog();
        }

        /// <summary>
        /// 导出规则
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void expToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int ID = int.Parse(treeView.SelectedNode.Name);
            FormImpExp formImpExp = new FormImpExp(false, Const.ClassType.Publish, ID);
            formImpExp.ShowDialog();
        }

        /// <summary>
        /// 树编辑后处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.Label == "" && e.Label == null)
            {
                e.CancelEdit = true;
                return;
            }

            // 如果选择规则
            if (treeView.SelectedNode.Name.Substring(0, 1) != "C")
            {
                PublishData publishData = treeView.SelectedNode.Tag as PublishData;
                DataHelper.UpdPublishName(publishData.ID, e.Label);
            }
            else
            {
                // 如果选择组
                ClassData classData = treeView.SelectedNode.Tag as ClassData;
                DataHelper.UpdClassName(classData.ClassID, e.Label);
            }
        }

        /// <summary>
        /// 树的菜单打开时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            // 如果选择规则
            if (treeView.SelectedNode.Name.Substring(0, 1) != "C")
            {
                delToolStripMenuItem.Enabled = true;
                expToolStripMenuItem.Enabled = true;
            }
            else
            {
                if (treeView.SelectedNode.Nodes != null && treeView.SelectedNode.Nodes.Count > 0)
                {
                    delToolStripMenuItem.Enabled = false;
                }
                else
                {
                    delToolStripMenuItem.Enabled = true;
                }

                expToolStripMenuItem.Enabled = false;
            }
        }






    }
}
